package com.zjq.designpatterns.creation.prototype.code.destorysingleton;/*
 * 版权信息: 北京千⾥马网信科技有限公司</br>
 * Copyright ©2008-2018. All rights reserved. 京ICP备16007318号
 */

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author jessie
 * @version 1.0
 * @date 2021/1/22
 */
public class Test {
    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        HungrySingleton hungrySingleton = HungrySingleton.getInstance();
        Method clone = hungrySingleton.getClass().getDeclaredMethod("clone");
        clone.setAccessible(true);
        HungrySingleton cloneHungrySingleton = (HungrySingleton) clone.invoke(hungrySingleton);
        //不是同一个对象，解决方法是要么我们的单例类不去实现Cloneable接口，或者实现了，需要在clone方法中return getInstance();
        System.out.println(hungrySingleton);
        System.out.println(cloneHungrySingleton);
    }
}
